<template>
	<view class="padding">
		<view class="cu-list padding bg-white margin-top">
			<view class="flex margin-top" :key='i.id' v-for="i in prizeList">
				<view class="margin-bottom">
					<view class="text-lg text-bold">{{i.prizeInfo.name}}</view>
					<!-- <view class="text-lg text-gray">{{i.chanceNo}}</view> -->
					<view class="text-gray text-df">
						中奖时间{{i.createTime}}
					</view>
					<view class="text-gray text-df">
						领取截至时间{{i.grantEndTime}}
					</view>
				</view>
				<view v-if="i.receive==1" class="text-orange text-green text-lg text-bold">
					已领取
				</view>
				<view v-else-if="isEndTime(i.grantEndTime)" class="text-gray text-lg text-bold">已过期</view>
				<view @tap='recivePrize(i.id)' v-else-if="i.prizeInfo.type==2" class="cu-btn round bg-orange">
					领取
				</view>
				<view @tap='open(i)' v-else class="cu-btn round bg-orange">
					查看券码
				</view>
			</view>
		</view>
		<u-popup v-model="show" mode="center">
			<view style="padding: 30rpx;">
				<image :src="'data:image/png;base64,'+src" mode="widthFix"></image>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {prizeList,recivePrize} from '@/api/api.js'
	export default {
		data() {
			return {
				prizeList:[],
				src:'',
				show:false
			};
		},
		onLoad() {
			this.getPrizeList();
		},
		methods:{
			recivePrize(id){
				recivePrize({id}).then(res=>{
					if(res.code==200){
						uni.showToast({
							icon:"success",
							title:"领取成功"
						})
						this.getPrizeList();
					}
				})
			},
			open(i){
				this.show = true;
				this.src = i.barCodeBase64;
			},
			getPrizeList(){
				prizeList().then(res=>{
					if(res.code==200){
						this.prizeList=res.data.map(item=>{
							item.prizeInfo = JSON.parse(item.prizeInfo);
							return item;
						})
					}
				})
			},
			isEndTime(endTime){
				return new Date(endTime).getTime()<new Date().getTime()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.cu-list{
		.flex{
			justify-content: space-between;
			align-items: center;
			border-bottom:solid 1rpx $u-border-color;
		}
		.margin-bottom{
			&>view{
				line-height: 1.5rem;
			}
		}
	}
</style>
